import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Cookie } from 'ng2-cookies';
import { AppConsts } from '../consts';

@Injectable()
export class AuthGuard implements CanActivate {
  constructor(private _router: Router, private _consts: AppConsts) {}

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    if (Cookie.check(this._consts.authCookieName)) {
      return true;
    }
    this._router.navigate(['/login']);
    return false;
  }
}
